<?php
namespace Cms\Controller;
use Cms\Controller\BaseController;
class LocksController extends BaseController {
	
	public function _initialize() {
		//你可以在此覆盖父类方法	
		parent::_initialize();
		$this->assign('useroath',$_SESSION['CMS']['user']['oath']);
		C("LOCKTYPE", array('10001'=>'NFC','10002'=>'蓝牙锁','10003'=>'指纹锁'));
		C("LOCKSTATUS", array('1'=>'正常','3'=>'维修','5'=>'报废'));
		$p=$_GET['p']?$_GET['p']:1;
		$this->assign('p',$p);
	}
	
	public function goodsList(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'资源管理',
				'url'=>U('Cms/Locks/#')
			),
			'1'=>array(
				'name'=>'智能锁管理',
				'url'=>U('Cms/Locks/goodsList')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//绑定搜索条件与分页
		$m=M('locks',"t_");
		$p=$_GET['p']?$_GET['p']:1;
		$this->assign('p',$p);
		$search=I('search')?I('search'):'';
		$stype=I('stype')?I('stype'):1;
		$lock_type = I("lock_type");
		$status = I("status");
		if($lock_type){
			$map['LockType'] = $lock_type;
			$this->assign('lock_type',$lock_type);
		}
		$map['Status'] = array('neq', 0);
		if($status){
			$map['Status'] = $status;
			$this->assign('status',$status);
		}
		if($stype==1){
			if($search){
				$map['SerialNo']=array('like',"%$search%");
				$this->assign('search',$search);
			}			
			$this->assign('stype',1);
		}
		if($stype==2){
			if($search){
				$map['Brand']=array('like',"%$search%");
				$this->assign('search',$search);
			}
			$this->assign('stype',2);
		}		
		if($_SESSION['CMS']['user']['CompanyId']) $map['CompanyId'] = $_SESSION['CMS']['user']['CompanyId'];
		$psize=self::$CMS['set']['pagesize']?self::$CMS['set']['pagesize']:20;
		$cache=$m->where($map)->page($p,$psize)->select();//echo D()->_sql();
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'Ystech-loader', '智能锁列表','Ystech-search');
		$this->assign('cache',$cache);
		$this->assign('count',$count);
		$this->assign('LOCKTYPE',C("LOCKTYPE"));
		$this->assign('LOCKSTATUS',C("LOCKSTATUS"));
		$this->display();
	}

	//智能锁设置
	public function goods(){
		$id=I('id');
		$m=M('locks',"t_");
		//dump($m);
		//设置面包导航，主加载器请配置		
		$bread=array(
			'0'=>array(
				'name'=>'资源管理',
				'url'=>U('Cms/Locks/#')
			),
			'1'=>array(
				'name'=>'智能锁列表',
				'url'=>U('Cms/Locks/goodsList')
			),
			'2'=>array(
				'name'=>'智能锁编辑',
				'url'=>$id?U('Cms/Locks/goods',array('id'=>$id)):U('Cms/Locks/goods')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//处理POST提交	
		if(IS_POST){
			$data=I('post.');
			$id = $data['ID'];
			if($this->check_dulplock($data['SerialNo'], $id)){
				$info['status']=0;
				$info['msg']='序列号重复！';
				$this->ajaxReturn($info);
			}
			
			$date = date("Y-m-d H:i:s");
			
			if($data['CreateDate']) $data['CreateDate'] = date("Y-m-d 00:00:00", strtotime($data['CreateDate']));
			if($data['ExpiryDate']) $data['ExpiryDate'] = date("Y-m-d 23:59:59", strtotime($data['ExpiryDate']));
			
			if($id){
				$re=$m->save($data);
				if(FALSE!==$re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}else{			
				$data['CreateTime'] = $date;
				if($_SESSION['CMS']['user']['CompanyId']) $data['CompanyId'] = $_SESSION['CMS']['user']['CompanyId'];
				$re=$m->add($data);
				if($re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}
			$this->ajaxReturn($info);
		}
		//处理编辑界面
		if($id){			
			$cache=$m->where('id='.$id)->find();
			$this->assign('cache',$cache);	
		}		
		$this->display();
	}
	
	public function check_dulplock($SerialNo,$id=''){
		$where = array('SerialNo'=>$SerialNo,'Status'=>array('neq',0));
		if($id) $where['ID'] = array('neq', $id);
		return M("locks","t_")->where($where)->count();
	}
	
	public function goodsDel(){
		$id=$_GET['id'];//必须使用get方法
		$m=M('locks','t_');
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		$ids = explode(",",$id);
		$re=$m->where(array('ID'=>array('in',$ids)))->save(array('Status'=>0));
		if($re){
			$info['status']=1;
			$info['msg']='删除成功!';
		}else{
			$info['status']=0;
			$info['msg']='删除失败!';
		}
		$this->ajaxReturn($info);
	}
	
	public function carrierList(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'资源管理',
				'url'=>U('Cms/Locks/#')
			),
			'1'=>array(
				'name'=>'载体管理',
				'url'=>U('Cms/Locks/carrierList')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//绑定搜索条件与分页
		$m=M('carrier',"t_");
		$p=$_GET['p']?$_GET['p']:1;
		$this->assign('p',$p);
		$search=I('search')?I('search'):'';
		$stype=I('stype')?I('stype'):1;
		
		$map['Status'] = array('neq', 0);
		if($stype==1){
			if($search){
				$map['SerialNo']=array('like',"%$search%");
				$this->assign('search',$search);
			}			
			$this->assign('stype',1);
		}
		if($stype==2){
			if($search){
				$map['Name']=array('like',"%$search%");
				$this->assign('search',$search);
			}
			$this->assign('stype',2);
		}		
		if($_SESSION['CMS']['user']['CompanyId']) $map['CompanyId'] = $_SESSION['CMS']['user']['CompanyId'];
		$psize=self::$CMS['set']['pagesize']?self::$CMS['set']['pagesize']:20;
		$cache=$m->where($map)->page($p,$psize)->select();//echo D()->_sql();		
		
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'Ystech-loader', '载体列表','Ystech-search');
		$this->assign('cache',$cache);
		$this->assign('count',$count);
		$this->display();
	}
	
	//锁修护日志
	public function goodsRepair(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'资源管理',
				'url'=>U('Cms/Locks/#')
			),
			'1'=>array(
				'name'=>'智能锁管理',
				'url'=>U('Cms/Locks/goodsList')
			),
			'2'=>array(
				'name'=>'智能锁维护',
				'url'=>$id?U('Cms/Locks/goodsRepair',array('id'=>$id)):U('Cms/Locks/goodsRepair')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//绑定搜索条件与分页
		$m=M('lockrepairlog',"t_");
		$p1=$_GET['p1']?$_GET['p1']:1;
		$this->assign('p1',$p);		
		$psize=self::$CMS['set']['pagesize']?self::$CMS['set']['pagesize']:20;
		if(I("id")) $SerialNo = M("locks","t_")->where(array('ID'=>I("id")))->getField("SerialNo");
		$map = array('Status'=>array('neq',0));
		if($SerialNo) $map['LockID'] = $SerialNo;
		$cache=$m->where($map)->page($p1,$psize)->select();//echo D()->_sql();
		
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'Ystech-loader', '维护','Ystech-search');
		$this->assign('cache',$cache);
		$this->assign('lockid',I("id"));
		$this->assign('count',$count);
		$this->display();
	}
	
	public function repair(){
		$id=I('ID');
		$m=M('lockrepairlog',"t_");
		//dump($m);
		//设置面包导航，主加载器请配置		
		$bread=array(
			'0'=>array(
				'name'=>'资源管理',
				'url'=>U('Cms/Locks/#')
			),
			'1'=>array(
				'name'=>'智能锁列表',
				'url'=>U('Cms/Locks/goodsList')
			),
			'2'=>array(
				'name'=>'维护',
				'url'=>$id?U('Cms/Locks/goodsRepair',array('id'=>$id)):U('Cms/Locks/goodsRepair')
			),
			'3'=>array(
				'name'=>'维护日志',
				'url'=>$id?U('Cms/Locks/repair',array('id'=>$id)):U('Cms/Locks/repair')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//处理POST提交	
		$p1=$_GET['p1']?$_GET['p1']:1;
		$this->assign('p1',$p);	
		if(IS_POST){
			$data=I('post.');
			unset($data['lock_id']);
			$date = date("Y-m-d H:i:s");
			if($data['RepairDate']) $data['RepairDate'] = date("Y-m-d 23:59:59", strtotime($data['RepairDate']));
			if($id){
				$re=$m->save($data);
				if(FALSE!==$re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}else{
				unset($data['ID']);
				$data['CreateTime'] = $date;
				if($_SESSION['CMS']['user']['CompanyId']) $data['CompanyId'] = $_SESSION['CMS']['user']['CompanyId'];
				if($_SESSION['CMS']['uid']) $data['UserID'] = $_SESSION['CMS']['uid'];
				$re=$m->add($data);//echo D()->_sql();
				if($re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}
			$this->ajaxReturn($info);
		}
		//处理编辑界面
		if($id){			
			$cache=$m->where('id='.$id)->find();
			if($cache['LockID']) {
				$lockinfo = M("locks","t_")->where(array("SerialNo"=>$cache['LockID']))->find();
			}
			$this->assign('cache',$cache);
		}else{
			$lockinfo = M("locks","t_")->where(array("ID"=>I('lock_id')))->find();	
		}
		$p1=$_GET['p1']?$_GET['p1']:1;
		$this->assign('p1',$p);	
		$this->assign('lockinfo',$lockinfo);
		$this->assign('lock_id',$lockinfo['ID']);
		$this->assign('LOCKTYPE',C("LOCKTYPE"));
		$this->display();
	}
	
	public function repairDel(){
		$id=$_GET['id'];//必须使用get方法
		$m=M('lockrepairlog','t_');
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		$ids = explode(",",$id);
		$re=$m->where(array('ID'=>array('in',$ids)))->save(array('Status'=>0));
		if($re){
			$info['status']=1;
			$info['msg']='删除成功!';
		}else{
			$info['status']=0;
			$info['msg']='删除失败!';
		}
		$this->ajaxReturn($info);
	}

	//智能锁设置
	public function carrier(){
		$id=I('id');
		$m=M('carrier',"t_");
		//dump($m);
		//设置面包导航，主加载器请配置		
		$bread=array(
			'0'=>array(
				'name'=>'资源管理',
				'url'=>U('Cms/Locks/#')
			),
			'1'=>array(
				'name'=>'载体列表',
				'url'=>U('Cms/Locks/carrierList')
			),
			'2'=>array(
				'name'=>'载体编辑',
				'url'=>$id?U('Cms/Locks/carrier',array('id'=>$id)):U('Cms/Locks/carrier')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//处理POST提交	
		if(IS_POST){
			$data=I('post.');
			$id = $data['id'];
			if($this->check_dulpcarrier($data['SerialNo'], $id)){
				$info['status']=0;
				$info['msg']='序列号重复！';
				$this->ajaxReturn($info);
			}
			
			$date = date("Y-m-d H:i:s");
			
			if($id){
				$re=$m->save($data);
				if(FALSE!==$re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}else{
				unset($data['id']);
				$data['CreateTime'] = $date;
				if($_SESSION['CMS']['user']['CompanyId']) $data['CompanyId'] = $_SESSION['CMS']['user']['CompanyId'];
				$re=$m->add($data);//echo D()->_sql();
				if($re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}
			$this->ajaxReturn($info);
		}
		//处理编辑界面
		if($id){			
			$cache=$m->where('id='.$id)->find();
			$this->assign('cache',$cache);	
		}		
		$this->display();
	}
	
	public function check_dulpcarrier($SerialNo,$id=''){
		$where = array('SerialNo'=>$SerialNo,'Status'=>array('neq',0));
		if($id) $where['id'] = array('neq', $id);
		if($_SESSION['CMS']['user']['CompanyId']) $where['CompanyId'] = $_SESSION['CMS']['user']['CompanyId'];
		return M("carrier","t_")->where($where)->count();
	}
	
	public function carrierDel(){
		$id=$_GET['id'];//必须使用get方法
		$m=M('carrier','t_');
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		$ids = explode(",",$id);
		$re=$m->where(array('id'=>array('in',$ids)))->save(array('Status'=>0));
		if($re){
			$info['status']=1;
			$info['msg']='删除成功!';
		}else{
			$info['status']=0;
			$info['msg']='删除失败!';
		}
		$this->ajaxReturn($info);
	}
	
	//载体加载智能锁设置
	public function carrier_lock(){
		$id=I('id');
		$m=M('lock_carrier',"t_");
		$lock=M('locks',"t_");
		$carrier=M('carrier',"t_");
		//dump($m);
		//设置面包导航，主加载器请配置		
		$bread=array(
			'0'=>array(
				'name'=>'资源管理',
				'url'=>U('Cms/Locks/#')
			),
			'1'=>array(
				'name'=>'载体列表',
				'url'=>U('Cms/Locks/carrierList')
			),
			'2'=>array(
				'name'=>'加载智能锁',
				'url'=>$id?U('Cms/Locks/carrier_lock',array('id'=>$id)):U('Cms/Locks/carrier_lock')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//处理POST提交	
		if(IS_POST){
			$data=I('post.');
			$id = $data['id'];
			if($this->check_dulpcarrierlock($data['CarrierId'], $id)){
				$info['status']=0;
				$info['msg']='智能锁已加载，请删除后再尝试';
				$this->ajaxReturn($info);
			}
			
			$date = date("Y-m-d H:i:s");
			
			if($data['EffectiveTime']) $data['EffectiveTime'] = date("Y-m-d 00:00:00", strtotime($data['EffectiveTime']));
			if($data['ExpiryDate']) $data['ExpiryDate'] = date("Y-m-d 23:59:59", strtotime($data['ExpiryDate']));
			
			if($id){
				$re=$m->save($data);
				if(FALSE!==$re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}else{
				unset($data['id']);
				$data['CreateTime'] = $date;
				if($_SESSION['CMS']['user']['CompanyId']) $data['CompanyId'] = $_SESSION['CMS']['user']['CompanyId'];
				$re=$m->add($data);//echo D()->_sql();
				if($re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}
			$this->ajaxReturn($info);
		}
		//处理编辑界面
		if($id){
			$carrierinfo = $carrier->where(array('id'=>$id))->find();
			$where = array('CarrierId'=>$carrierinfo['SerialNo'], 't_lock_carrier.Status'=>array('neq', 0));
			if($_SESSION['CMS']['user']['CompanyId']) $where['t_lock_carrier.CompanyId'] = $_SESSION['CMS']['user']['CompanyId'];
			$join = "t_locks ON t_locks.SerialNo=t_lock_carrier.LockId";
			$cache=$m->join($join)->where($where)->field("t_lock_carrier.*,t_locks.LockType")->find();
			$cache['SerialNo'] = $carrierinfo['SerialNo'];
			$cache['Name'] = $carrierinfo['Name'];
			$this->assign('cache',$cache);
			$wh = array('_string'=>"SerialNo not in (SELECT LockId FROM `t_lock_carrier` WHERE Status<>0) AND SerialNo<>'".$cache['LockId']."' AND Status in (1,3)");
			if($_SESSION['CMS']['user']['CompanyId']) $wh['CompanyId'] = $_SESSION['CMS']['user']['CompanyId'];
			$lockinfo = $lock->where($wh)->select();
			$this->assign('lockinfo',$lockinfo);
			$this->assign('LOCKTYPE',C("LOCKTYPE"));
		}		
		$this->display();
	}
	
	public function check_dulpcarrierlock($SerialNo,$id=''){
		$where = array('CarrierId'=>$SerialNo,'Status'=>array('neq',0));
		if($id) $where['id'] = array('neq', $id);
		if($_SESSION['CMS']['user']['CompanyId']) $where['CompanyId'] = $_SESSION['CMS']['user']['CompanyId'];
		return M("lock_carrier","t_")->where($where)->count();
	}
	
	public function carrierlockDel(){
		$id=$_GET['id'];//必须使用get方法
		$m=M('lock_carrier','t_');
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
//		$ids = explode(",",$id);
		$re=$m->where(array('id'=>$id))->save(array('Status'=>0));
		if($re){
			$info['status']=1;
			$info['msg']='删除成功!';
		}else{
			$info['status']=0;
			$info['msg']='删除失败!';
		}
		$this->ajaxReturn($info);
	}
	
	public function orderlist(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'资源管理',
				'url'=>U('Cms/Locks/#')
			),
			'1'=>array(
				'name'=>'使用人员管理',
				'url'=>U('Cms/Locks/orderlist')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//绑定搜索条件与分页
		$m=M('carrier_user',"t_");
		$p=$_GET['p']?$_GET['p']:1;
		$this->assign('p',$p);
		$search=I('search')?I('search'):'';
		$stype=I('stype')?I('stype'):1;
		$status = I("status");
		$statusArr = array("2"=>"正常","1"=>"已删除");
		if(key_exists($status,$statusArr)){
			$map['t_carrier_user.Status'] = $status-1;
			$this->assign('status',$status);
		}
		
		$st = I("st");
		if($st){
			$date = date('Y-m-d H:i:s');
			switch ($st){
				case '1': $map['Isuse'] = 0; $map['EffectiveTime'] = array("lt",$date); $map['ExpiryTime'] = array("gt",$date); break;//未使用
				case '2': $map['Isuse'] = 1; break;//已使用
				case '3': $map['Isuse'] = 0; $map['ExpiryTime'] = array("lt",$date); //未使用已过期
			}
		}
		if($stype==1){
			if($search){
				$map['t_carrier_user.CarrierId']=array('like',"%$search%");
				$this->assign('search',$search);
			}			
			$this->assign('stype',1);
		}
		if($stype==2){
			if($search){
				$map['t_carrier.Name']=array('like',"%$search%");
				$this->assign('search',$search);
			}
			$this->assign('stype',2);
		}
		if($stype==3){
			if($search){
				$map['Ystech_vip.nickname']=array('like',"%$search%");
				$this->assign('search',$search);
			}
			$this->assign('stype',3);
		}		
		if($_SESSION['CMS']['user']['CompanyId']) $map['t_carrier_user.CompanyId'] = $_SESSION['CMS']['user']['CompanyId'];
		$psize=self::$CMS['set']['pagesize']?self::$CMS['set']['pagesize']:20;
		$join = array("t_carrier on t_carrier.SerialNo=t_carrier_user.CarrierId","Ystech_vip on Ystech_vip.id=t_carrier_user.UserID");
		$field = "t_carrier_user.*,t_carrier.Name,Ystech_vip.nickname,Ystech_vip.name truename";
		$cache=$m->join($join)->where($map)->page($p,$psize)->field($field)->order("EffectiveTime DESC")->select();//echo D()->_sql();
		$count=$m->join($join)->where($map)->count();
		$this->getPage($count, $psize, 'Ystech-loader', '使用人员管理','Ystech-search');
		$this->assign('cache',$cache);
		$this->assign('count',$count);
		$this->assign('LOCKTYPE',C("LOCKTYPE"));
		$this->assign('LOCKSTATUS',C("LOCKSTATUS"));
		C('STATUS',$statusArr);
		$this->display();
	}
	
	public function order(){
		$id=I('id');
		$m=M('carrier_user',"t_");
		//dump($m);
		//设置面包导航，主加载器请配置		
		$bread=array(
			'0'=>array(
				'name'=>'资源管理',
				'url'=>U('Cms/Locks/#')
			),
			'1'=>array(
				'name'=>'使用人员管理',
				'url'=>U('Cms/Locks/goodsList')
			),
			'2'=>array(
				'name'=>'使用人员编辑',
				'url'=>$id?U('Cms/Locks/order',array('id'=>$id)):U('Cms/Locks/order')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//处理POST提交	
		if(IS_POST){
			$data=I('post.');
			
			$date = date("Y-m-d H:i:s");
			
			if($data['EffectiveTime']) $data['EffectiveTime'] = date("Y-m-d H:i:00", strtotime($data['EffectiveTime']));
			if($data['ExpiryTime']) $data['ExpiryTime'] = date("Y-m-d H:i:59", strtotime($data['ExpiryTime']));
			if(strtotime($data['EffectiveTime'])>=strtotime($data['ExpiryTime'])){
				$info['status']=0;
				$info['msg']='生效日期不得大于失效日期！';
				$this->ajaxReturn($info);
			}
			
			if($id){
				$re=$m->save($data);
				if(FALSE!==$re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}else{
				$data['CreateTime'] = $date;
				$infoArr = M("lock_carrier","t_")->where(array("CarrierId"=>$data['CarrierId']))->field("LockId,CompanyId")->find();
				$data['LockId'] = $infoArr['LockId'];
				if(!$data['LockId']){
					$info['status']=0;
					$info['msg']='载体未绑定智能锁！';
					$this->ajaxReturn($info);
				}
				$data['CompanyId'] = $infoArr['CompanyId'];
				if($_SESSION['CMS']['uid']) $data['OperateID'] = $_SESSION['CMS']['uid'];
				$re=$m->add($data);
				if($re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}
			$this->ajaxReturn($info);
		}
		//处理编辑界面
		if($id){			
			$cache=$m->join(array("t_carrier ON t_carrier.SerialNo=t_carrier_user.CarrierId","t_lock_carrier ON t_lock_carrier.CarrierId=t_carrier_user.CarrierId"))->where('t_carrier_user.id='.$id)->field("t_carrier_user.*,t_carrier.Name,t_lock_carrier.LockId")->find();//echo D()->_sql();
			if($cache){
				$user = M("vip")->where(array('id'=>$cache['UserID']))->field("name,nickname,mobile")->find();
				$cache['username'] = $user['nickname'];
				$cache['mobile'] = $user['mobile'];
			}
			$this->assign('cache',$cache);
		}
		$wh = array('t_carrier.Status'=>1,'t_lock_carrier.Status'=>1);
		if($_SESSION['CMS']['user']['CompanyId']) $wh['t_lock_carrier.CompanyId'] = $_SESSION['CMS']['user']['CompanyId'];		
		$carrierinfo = M("lock_carrier","t_")->join("t_carrier ON t_carrier.SerialNo=t_lock_carrier.CarrierId")->where($wh)->field("t_carrier.*,t_lock_carrier.LockId")->select();
		$this->assign('carrierinfo', $carrierinfo);
		$this->display();
	}
	
	public function usertab(){
		$name = I("search");
		if($name) $info = M("Vip")->where(array('nickname'=>array('like',"%".$name."%")))->select();
		$this->assign('cache', $info);
		$this->display();
	}
	
	public function orderDel(){
		$id=$_GET['id'];//必须使用get方法
		$m=M('carrier_user','t_');
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		$ids = explode(",",$id);
		$re=$m->where(array('id'=>array('in',$ids)))->save(array('Status'=>0));
		if($re){
			$info['status']=1;
			$info['msg']='删除成功!';
		}else{
			$info['status']=0;
			$info['msg']='删除失败!';
		}
		$this->ajaxReturn($info);
	}
	
	/**
	 * 导出数据为excel表格
	 *@param $data    一个二维数组,结构如同从数据库查出来的数组
	 *@param $title   excel的第一行标题,一个数组,如果为空则没有标题
	 *@param $filename 下载的文件名
	 *@examlpe
	 $stu = M ('User');
	 $arr = $stu -> select();
	 exportexcel($arr,array('id','账户','密码','昵称'),'文件名!');
	 */
	private function exportexcel($data = array(), $title = array(), $filename = 'report') {
		header("Content-type:application/octet-stream");
		header("Accept-Ranges:bytes");
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:attachment;filename=" . $filename . ".xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		//导出xls 开始
		if (!empty($title)) {
			foreach ($title as $k => $v) {
				$title[$k] = iconv("UTF-8", "GB2312", $v);
			}
			$title = implode("\t", $title);
			echo "$title\n";
		}
		if (!empty($data)) {
			foreach ($data as $key => $val) {
				foreach ($val as $ck => $cv) {
					$data[$key][$ck] = iconv("UTF-8", "GB2312", $cv);
				}
				$data[$key] = implode("\t", $data[$key]);

			}
			echo implode("\n", $data);
		}

	}
	
}